package modes

import (
	"errors"
	"fmt"
	"time"
)

/*
 * 描述：工作池, 工作池的编号对应校区ID,主要功能是管理每个招生老师的自己的工作池
 *
 ********************************************************************************************/
type WorkPool struct {
	Id      int64  `json:"wp_id" xorm:"id"`                 // 公海id
	UsingId int64  `json:"wp_using_id" xorm:"using_id"`     // 公海池id
	Phone   int64  `json:"wp_phone" xorm:"phone"`           // 手机号
	UNote   string `json:"wp_u_note" xorm:"u_note"`         // 备注
	Uid     int64  `json:"wp_uid" xorm:"uid"`               // 招生老师编号
	CallCou int64  `json:"wp_call_count" xorm:"call_count"` // 拨打次数
	CallAt  int64  `json:"wp_call_at" xorm:"call_at"`       // 最近一次的拨打
	PId     int64  `json:"wp_pid" xorm:"p_id"`              // 项目ID
	PName   string `json:"wp_pname" xorm:"p_name"`          // 项目名称
	At      int64  `json:"wp_at" xorm:"at"`                 // 领取时间
	City    int64  `json:"wp_city" xorm:"city"`             // 城市ID
	State   int64  `json:"wp_state" xorm:"state"`           // 状态
	Weight  int64  `json:"wp_weight" xorm:"weight"`         // 权重
}
type JoinWork struct {
	WorkPool `xorm:"extends"`
	Employee `xorm:"extends"`
}
type JoinCall struct {
	CallPhone `xorm:"extends"`
	WorkPool  `xorm:"extends"`
	Employee  `xorm:"extends"`
}

func (this *WorkPool) TableName() string {
	return "work_pool"
}

func (this *WorkPool) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(2).Insert(this)
}

func (this *WorkPool) Get() (bool, error) {
	return Db(2).Get(this)
}

func (this *WorkPool) update(where string, field string) (int64, error) {
	return Db(2).Where(where).Cols(field).Update(this)
}

func (this *WorkPool) Count(where string) (int64, error) {
	var val WorkPool
	return Db(2).Where(where).Count(&val)
}

func (this *WorkPool) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *WorkPool) IdDel() (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.delete(where)
}

func (this *WorkPool) delete(where string) (int64, error) {
	return Db(2).Where(where).Delete(this)
}

func (this *WorkPool) where(fage, count, page int, where, field string) ([]WorkPool, error) {
	list := make([]WorkPool, 0)
	var err error
	if field == "" {
		field = "id"
	}
	page--
	if 0 == fage { // 从小到大排序
		err = Db(2).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(2).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

func (this *WorkPool) List(val *Pager) error {
	val.TName = this.TableName()
	val.Total, _ = this.Count(val.ToWhere())
	var field string
	switch val.Coll {
	case 0:
		field = "at" // 领取时间
	case 1:
		field = "call_at" // 最近一次的拨打时间
	case 2:
		field = "call_count" // 拨打次数
	case 3:
		field = "weight" // 权重
	case 4:
		field = "p_id" // 按项目排序
	default:
		return errors.New("排序规则错误")
	}
	list, err := this.where(val.Rules, val.Count, val.Page, val.ToWhere(), field)
	if err == nil {
		val.List = list
	}
	return err
}

func (this *WorkPool) PhoneDel(phone int64) (int64, error) {
	return Db(2).Where("phone = ?", phone).Delete(this)
}

func (this *JoinWork) NList(val *Pager) error {
	list := make([]JoinWork, 0)
	val.TName = "work_pool"
	val.Page--
	if val.Page <= 0 {
		val.Page = 0
	}
	var field, sort string

	field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}

	val.Total, _ = Db(2).Table(val.TName).
		Join("INNER", "employee", "work_pool.uid = employee.id").
		Where(val.ToWhere()).
		Count(this)
	err := Db(2).Table(val.TName).
		Join("INNER", "employee", "work_pool.uid = employee.id").
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}

/*
 * 描述: 电话记录列表
 *
 ***************************************************************************************/
func (this *JoinCall) PList(uid int64, val *Pager) error {
	list := make([]JoinCall, 0)
	val.TName = "call_record"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) // 拨打时间
	case 1:
		field = fmt.Sprintf("%s.duration", val.TName) // 通话时长
	case 2:
		field = fmt.Sprintf("%s.pid", val.TName) // 项目
	case 3:
		field = fmt.Sprintf("%s.fage", val.TName) // 标签
	case 4:
		field = fmt.Sprintf("%s.state", val.TName) // 状态
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 拨打时间
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(2).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(2).Table(val.TName).
		Join("INNER", "work_pool", fmt.Sprintf("%s.phone = work_pool.phone", val.TName)).
		Join("INNER", "employee", fmt.Sprintf("work_pool.uid = employee.id")).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}
